﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RailwayReservationWeb.manager
{
    public partial class profile_changepwd : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new RailwayReservationDataContext();
            pMessage.Visible = false;
        }

        private void DisplayMessage(string msg)
        {
            pMessage.InnerText = msg;
            pMessage.Visible = true;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;
            string oldpass = Checksum.MD5(txtOldPass.Text);
            string newpass = Checksum.MD5(txtNewPass.Text);
            string confpass = Checksum.MD5(txtConfPass.Text);
            if (!confpass.Equals(newpass)) return;
            Account current;
            if (Session["authinfo"] != null)
                current = (Account)Session["authinfo"];
            else return;
            Account account = db.Accounts.FirstOrDefault(
                a => a.AccountId == current.AccountId
                && a.UserName == current.UserName
                && a.Password == oldpass);

            if (account == null)
            {
                DisplayMessage("Old Password is not valid");
                return;
            }
            account.Password = newpass;
            try
            {
                db.SubmitChanges();
                // DisplayMessage("Changes to your account information have been saved successfully.");
                Response.Redirect("~/manager/logout.aspx"); // "?rdr=~/manager/default.aspx");
            }
            catch (Exception ex)
            {
                DisplayMessage(ex.Message);
            }
        }
    }
}